app-id: io.github.fm_elpac.pmim_ibus
runtime: org.freedesktop.Platform
runtime-version: '23.08'
sdk: org.freedesktop.Sdk

base: org.electronjs.Electron2.BaseApp
base-version: '23.08'
separate-locales: false

command: run.sh
finish-args:
  - --share=ipc
  - --socket=x11
  - --socket=pulseaudio
  - --share=network
  - --device=dri
  - --filesystem=xdg-run/pmim:create
  #- --filesystem=xdg-config/pmim:create

modules:
  - name: pmim-ibus
    buildsystem: simple
    build-commands:
      # DEBUG
      - pwd
      - ls -l
      - ls -l pmim-ibus
      - ls -l ui/dist
      - ls -l server
      - ls -l dist-deno
      #- ls -l dist-electron
      - ls -l dist-librush
      #- ls -l pmim-data

      # files for desktop app
      - install -D -t /app/share/metainfo/ pmim-ibus/flatpak/io.github.fm_elpac.pmim_ibus.metainfo.xml
      - install -D -t /app/share/applications/ pmim-ibus/flatpak/io.github.fm_elpac.pmim_ibus.desktop
      - install -D pmim-ibus/logo/pmim-2024-2.svg /app/share/icons/hicolor/scalable/apps/io.github.fm_elpac.pmim_ibus.svg

      - install -D -t /app/ pmim-ibus/LICENSE
      - install -D -t /app/ pmim-ibus/README.md

      # /app/bin
      - install -Dm755 -t /app/bin/ run.sh
      - install -Dm755 -t /app/bin/ pmim-ibus/flatpak/restart.sh

      - install -Dm755 -t /app/bin/ dist-deno/deno

      - |
        if [ "$FLATPAK_ARCH" == "x86_64" ]; then
          install -Dm755 -t /app/bin/ dist-librush/release/ibrus
        fi
      - |
        if [ "$FLATPAK_ARCH" == "aarch64" ]; then
          install -Dm755 -t /app/bin/ dist-librush/aarch64-unknown-linux-gnu/release/ibrus
        fi

      - cp pmim-ibus/flatpak/pmim_ibrus.xml /app

      # /app/main *.js
      - mkdir -p /app/main
      - cp pmim-ibus/electronjs/*.js /app/main

      # pmim-server
      - cp -r server /app/
      # ui-vue-dist
      - cp -r ui/dist/* /app/server/static/

      # plugin
      - mkdir -p /app/server/plugin
      - cp -r pmim-ibus/plugin/* /app/server/plugin
      # pmim-uis-nc
      - mkdir -p /app/server/plugin/pmim-uis-nc/static
      - cp -r pmim-ibus/uis/pmim-uis-nc/dist/* /app/server/plugin/pmim-uis-nc/static
      # pmim-uis-bl
      - mkdir -p /app/server/plugin/pmim-uis-bl/static
      - cp -r pmim-ibus/uis/pmim-uis-bl/dist/* /app/server/plugin/pmim-uis-bl/static

      # electron dist
      - mkdir -p /app/dist-electron
      - cp dist-electron.zip /app/dist-electron
      - cd /app/dist-electron && unzip dist-electron.zip
      - rm /app/dist-electron/dist-electron.zip
      #- cp -r dist-electron /app

      # pmim-data
      - zstd -d pmim_sys-0.db.zst
      - cp pmim_sys-0.db /app

      # DEBUG
      - find /app

    sources:
      - type: script
        dest-filename: run.sh
        commands:
          - env PMIMS_DB=${XDG_CONFIG_HOME}/pmim PMIMS_PLUGIN=${XDG_CONFIG_HOME}/pmim/plugin zypak-wrapper.sh /app/dist-electron/electron /app/main/main.js "$@"

      # pmim-ibus
      - type: dir
        path: ../
        dest: pmim-ibus

      # pmim-ibus/ui-vue-dist
      - type: dir
        path: ../ui-vue
        dest: ui

      # pmim-server
      - type: archive
        dest: server
        path: ./build_src/pmim-server.tar.zst
        sha256: 8b63350669bfc25bbedcb6a87f0af1ab8630e5e2ea356acef8bf034450da0e79

      # deno
      - type: archive
        dest: dist-deno
        only-arches: [ "x86_64" ]
        path: ./build_src/deno-x86_64-unknown-linux-gnu.zip
        sha256: 8102989679275f1c9859fd963ef407f60da0274337fbb75290b3e9df8174a30b

      # electronjs dist
      - type: file
      #- type: dir
        dest-filename: dist-electron.zip
        only-arches: [ "x86_64" ]
        #path: node_modules/electron/dist
        path: ./build_src/electron.zip
        sha256: f44e33a29019ce7f12a78d7dc376b467907f71936323feadae5a8ad932951856

      # ibrus
      - type: archive
        dest: dist-librush
        only-arches: [ "x86_64" ]
        path: ./build_src/librush_release_x86_64-unknown-linux-gnu.tar.zst
        sha256: 8af4ae4d927b3a2a88a8e8dbb4448bde0c384d509bc838c4aaeaabc24525906e

      # pmim-data/pmim_sys-0.db.zst
      - type: file
        dest-filename: pmim_sys-0.db.zst
        path: ./build_src/pmim_sys-0.db.zst
        sha256: e4d424c095c3bcbff2d0a9c2aa4bca6dde8185454d18d4c96fa058781a54ef60

# TODO
